/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2009 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.pages.user.register;

import java.io.Serializable;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import es.sonxurxo.androidrunner.model.persistence.util.exceptions.DuplicateInstanceException;
import es.sonxurxo.androidrunner.model.service.player.to.PlayerRegisterTO;
import es.sonxurxo.androidrunner.view.web.application.AndroidRunnerApplication;
import es.sonxurxo.androidrunner.view.web.pages.BasePage;
import es.sonxurxo.androidrunner.view.web.session.AndroidRunnerSession;

/**
 * @author "Xurxo Mendez Perez"
 *
 */
public class RegisterPage extends BasePage {

	public RegisterPage() {
		super();
		add(new RegisterForm("registerform", new Model(new SimpleUser())));

        if (AndroidRunnerSession.get().isSignedIn()) {
            error(getLocalizer().getString("registerPage.errors.alreadysignedin", RegisterPage.this));
        }
	}
	
	public RegisterPage(PageParameters pageParameters) {
		this();
	}
	
	public final class RegisterForm extends Form {

		private static final long serialVersionUID = 7986802625710098835L;

		public RegisterForm(String id, IModel model) {
            super(id, new CompoundPropertyModel(model));
            add(new TextField("login").setRequired(true));
            add(new PasswordTextField("password").setResetPassword(true));
            add(new PasswordTextField("retypePassword").setResetPassword(true));
            add(new TextField("name").setRequired(true));
            add(new TextField("surname").setRequired(true));
            add(new TextField("email").setRequired(true));
            add(new TextField("country").setRequired(true));
            add(new TextField("phone").setRequired(true));
            add(new CheckBox("showPersonalInfo"));
        }

        /**
         * Called upon form submit. Attempts to authenticate the user.
         */
        @Override
		protected void onSubmit() {

        	SimpleUser user = ((SimpleUser) getModel().getObject());
            String login = user.getLogin();
            String password = user.getPassword();
            String retypePassword = user.getRetypePassword();
            if (!password.equals(retypePassword)) {
            	error(getLocalizer().getString("registerPage.errors.passwordAndRetypedPasswordDontMatch", RegisterPage.this));
            }
            String name = user.getName();
            String surname = user.getSurname();
            String email = user.getEmail();
            String country = user.getCountry();
            String phone = user.getPhone();
            Boolean showPersonalInfo = new Boolean(user.isShowPersonalInfo());
                
            PlayerRegisterTO playerInfoTO = new PlayerRegisterTO(login, password, email, phone, name, surname,
           		country, showPersonalInfo.booleanValue(), 0, 0, 0, 0);
            try {
            	AndroidRunnerApplication.get().getPlayerService().register(playerInfoTO);
	            // Attempt to authenticate.
            	if (AndroidRunnerSession.get().signIn(login, password)) {
            		setResponsePage(getApplication().getHomePage());
            	} else {
            		error(getLocalizer().getString("register.errors.errorRegistering", RegisterPage.this));
            	}
            } catch (DuplicateInstanceException e) {
            	// TODO
            }
                
            setRedirect(true);
        }
    }
	
	public static class SimpleUser implements Serializable {

		private static final long serialVersionUID = 3386918253102133179L;
		
		private String login;
    	private String password;
    	private String retypePassword;
    	private String email;
    	private boolean showPersonalInfo;
    	private String phone;
    	private String name;
    	private String surname;
    	private String country;
    	
    	public String getLogin() {
    		return this.login;
    	}
    	
    	public void setLogin(String login) {
    		this.login = login;
    	}
    	
    	public String getPassword() {
    		return this.password;
    	}
    	
    	public void setPassword(String password) {
    		this.password = password;
    	}

		public String getRetypePassword() {
			return this.retypePassword;
		}

		public void setRetypePassword(String retypePassword) {
			this.retypePassword = retypePassword;
		}

		public String getEmail() {
			return this.email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public boolean isShowPersonalInfo() {
			return this.showPersonalInfo;
		}

		public void setShowPersonalInfo(boolean showPersonalInfo) {
			this.showPersonalInfo = showPersonalInfo;
		}

		public String getPhone() {
			return this.phone;
		}

		public void setPhone(String phone) {
			this.phone = phone;
		}

		public String getName() {
			return this.name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getSurname() {
			return this.surname;
		}

		public void setSurname(String surname) {
			this.surname = surname;
		}

		public String getCountry() {
			return this.country;
		}

		public void setCountry(String country) {
			this.country = country;
		}
	}

	@Override
	protected String getTitle() {
		return getLocalizer().getString("registerPage.title", RegisterPage.this);
	}
}
